package com.rimi.servlet;

import com.rimi.bean.JsonResult;
import com.rimi.bean.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Set;

/**
 * @author chengliang
 * 创建时间 2020/3/24 14:49
 */
@WebServlet("/online")
public class OnlineServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Set<User> onlineUser = (Set<User>) request.getServletContext().getAttribute("onlineUser");
        PrintWriter writer = response.getWriter();
        writer.append(JsonResult.SUCCESS(onlineUser).append("count",onlineUser.size()).toJson());
        writer.flush();
        writer.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
